package com.mjx.Util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * Created by meijingxiang on 2017/3/14.
 */
public class BeanFactory implements ApplicationListener<ContextRefreshedEvent> {
    private static ApplicationContext applicationContext;

    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (applicationContext == null){
            applicationContext = contextRefreshedEvent.getApplicationContext();
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        BeanFactory.applicationContext = applicationContext;
    }
}
